package com.ny.view;

import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.ProductService;
import com.ny.service.StudentService;
import com.ny.service.impl.ProductServiceImpl;
import com.ny.service.impl.StudentServiceImpl;

import java.util.List;
import java.util.Scanner;

public class IndexView {
    // 1.创建业务层的对象
    private static StudentService studentService = new StudentServiceImpl();
    private static ProductService productService=new ProductServiceImpl();

    public static void main(String[] args) throws Exception {
        boolean flag = true;// 循环状态的判断
        while (flag) {
            System.out.println("欢迎来到校园商品系统");
            System.out.println("请选择");
            System.out.println("1.用户登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            switch (n) {
                case 1:
                    System.out.println("请登录");
                    System.out.println("请输入用户名");
                    String username = scanner.next();
                    System.out.println("请输入密码");
                    String password = scanner.next();
                    // 调用业务层的登录方法，返回一个 student 对象（使用 student 接收结果）
                    Student student = studentService.login(username, password);
                    // 判断输入的用户名和密码是否等于数据库的用户名和密码
                    if (student != null) {
                        System.out.println("登录成功：来到商品显示界面");
                        // 查询所有的商品信息出来
                        List<Product> productList = productService.findAllProduct();
                        System.out.print("商品序号" + "\t" + "\t");
                        System.out.print("商品名称" + "\t" + "\t");
                        System.out.print("商品库存" + "\t" + "\t");
                        System.out.print("商品种类" + "\t" + "\t");
                        System.out.print("商品介绍" + "\t" + "\t");
                        System.out.println();
                        // 对集合进行遍历
                        for (Product product : productList) {
                            System.out.print(product.getProductId() + "\t" + "\t");
                            System.out.print(product.getProductName() + "\t" + "\t");
                            System.out.print(product.getStock() + "\t" + "\t");
                            System.out.print(product.getPrice() + "\t" + "\t");
                            System.out.print(product.getCategory() + "\t" + "\t");
                            System.out.println(product.getDescription());
                        }
                    } else {
                        System.out.println("重新登录");
                    }
                    break;
                case 2:
                    System.out.println("请注册");
                    Student s = new Student();
                    System.out.println("请输入用户名");
                    String name = scanner.next();
                    System.out.println("请输入手机号");
                    String phone = scanner.next();
                    System.out.println("请输入密码");
                    String pwd = scanner.next();

                    s.setName(name);
                    s.setPhone(phone);
                    s.setPassword(pwd);

                    int i = studentService.insertStudent(s);
                    if (i > 0) {
                        System.out.println("注册成功");
                    } else {
                        System.out.println("注册失败");
                    }
                    break;
                case 3:
                    System.out.println("退出系统");
                    flag = false;
                    break;
                default:
                    System.out.println("你输入正确的选择序号");
            }
        }
    }
}